(function (e) {
    let profile = localStorage.getItem('profile');
    // console.log(JSON.parse(profile));
    // 注册全局组件
    Vue.component('music', {
        template: '#musicPlayer'
    })
    const musicUrl = {

    }
    // 搜索歌曲功能
    console.log($('#search')[0]);
    const search = new Vue({
        el: '#search',
        data: {
            songName: '',
            showKeyword: '',
            songArr: [],
            musicUrl: ''
        },
        methods: {
            getSong() {
                // 搜索
                if (!this.songName.length) {
                    getEl.toast_error('内容给不能为空哦~');
                    this.songArr = []
                    return false;
                } else {
                    getEl.toast_success('已为你匹配到30首歌曲~');
                }

                axios({
                    url: '/cloudsearch',
                    params: {
                        keywords: this.songName
                    }
                }).then(res => {
                    console.log('搜索歌曲');
                    console.log(res.data.result.songs);
                    this.songArr = res.data.result.songs
                    this.songArr = res.data.result.songs.filter((item, index, selfArr) => {
                        return item.name.match(this.songName)
                    });
                    let reg = new RegExp(this.songName, 'ig')
                    let itemArray = this.songArr.map((item, index, selfArr) => {
                        return {
                            id: item.id,
                            publishTime: item.publishTime, //发布时间 
                            name: item.name.replace(reg, '<strong style="color:red;font-weight:bold">' + this.songName + '</strong>')

                        }
                    });
                    console.log(itemArray);
                    this.songArr = itemArray;

                });
            },
            //默认搜索关键词
            getShowKeyword() {
                axios({
                    url: '/search/default'
                }).then(res => {
                    console.log('默认搜索关键词');
                    console.log(res.data.data.showKeyword);
                    this.showKeyword = res.data.data.showKeyword
                })
            },
            // 点击开始播放音乐
            playMusic(id) {
                console.log(id);
                // 检测音乐是否可用
                axios({
                        url: '/check/music',
                        params: {
                            id
                        }
                    }).then(res => {
                        console.log('检测音乐是否可用');
                        console.log(res.data);
                        let {
                            message,
                            success
                        } = res.data
                        return Promise.resolve(res.data);
                    }).then(res => {
                        console.log('我是第二个then');
                        if (res.success) {
                            // 获取音乐 url
                            axios({
                                url: '/song/url',
                                params: {
                                    id
                                }
                            }).then(res => {
                                console.log('获取音乐 url');
                                console.log(res.data.data[0].url);
                                musicUrl.mp3 = res.data.data[0].url;
                                console.log(musicUrl);
                                console.log(this); //vue实例对象
                                return Promise.resolve(musicUrl)
                            }).then(res => {
                                console.log('我是第三个then');
                                console.log(res);
                                this.musicUrl = musicUrl.mp3;
                                // 携带跳转至播放音乐界面
                                location.href = '../song2.html?id=' + id + '';
                                //  播放器初始化 
                                musicList = [
                                    musicUrl,
                                ];
                                console.log(musicList);
                            });
                        }
                        // 

                    })
                    .catch(err => {
                        console.error(err);
                    });

            }

        },
        filters: {
            dateTime(time) {
                /* 输入总毫秒数，返回日期对象 */
                function formatDate(shijianchuo) {
                    //补零函数
                    function add0(m) {
                        return m < 10 ? '0' + m : m
                    }
                    var time = new Date(shijianchuo);
                    var y = time.getFullYear();
                    var m = time.getMonth() + 1;
                    var d = time.getDate();
                    var h = time.getHours();
                    var mm = time.getMinutes();
                    var s = time.getSeconds();
                    return y + '-' + add0(m) + '-' + add0(d);
                }
                return formatDate(time)
            }
        },
        computed: {

        },
        created() {
            this.getShowKeyword()

        },

    });
    const music = new Vue({
        el: '#app',
        data: {
            msg: '111111111'
        }
    });
    var musicList = null;
   
    // 用于获取音乐url的函数
    function getMusicUrl(id) {
        let url = '';
        axios({
                url: '/check/music',
                params: {
                    id
                }
            }).then(res => {
                console.log('检测音乐是否可用');
                console.log(res.data);
                let {
                    message,
                    success
                } = res.data
                return Promise.resolve(res.data);
            }).then(res => {
                console.log('函数我是第二个then');
                if (res.success) {
                    // 获取音乐 url
                    axios({
                        url: '/song/url',
                        params: {
                            id
                        }
                    }).then(res => {
                        console.log('函数获取音乐 url');
                        console.log(res.data.data[0].url);
                        url = res.data.data[0].url
                    })
                }

            })
            .catch(err => {
                console.error(err);
            });
        return url
    }
}())